local ELEMENT = {}
dhlite.PrepareElement( ELEMENT, "phantom" )

ELEMENT.Name         = "Phantom"
ELEMENT.DefaultOn    = true

ELEMENT.Description = ""

ELEMENT.HOOK = {}

function ELEMENT:Load()
	self.myMaterial = Material( "proxi/beacon_flare" )
	self.color = Color( 192, 128, 255, 255 )
	
end

function ELEMENT:Perform( )	
	if not self.Mesh then
		self.LastMeshTime = 0
		self.MeshDelay = 0
		self.MaxDist = 720
		
		self.Spacing = 1
		
		self.MeshDetail = 1024
		self.Mesh = {}
		local opos = LocalPlayer():GetShootPos() + LocalPlayer():GetAimVector() * 256
		for i=1,self.MeshDetail do
			self.Mesh[i] = opos
		end
		
	end

	render.SetMaterial( self.myMaterial )

	local radiusReference = 16
	local div = 32
	local cycle = ((SysTime() * 1) % div)
	local mouseVect = LocalPlayer():GetShootPos() + LocalPlayer():GetAimVector() * 256
	
		local distance = (mouseVect - self.Mesh[1]):Length()
		while distance > self.Spacing do
		
			local backy = table.remove(self.Mesh, self.MeshDetail)
			backy = self.Mesh[1] + (mouseVect - self.Mesh[1]):Normalize() * self.Spacing
			table.insert(self.Mesh, 1, backy)
		
			distance = distance - self.Spacing 
		end
	
	//render.DrawSprite( mouseVect.x, mouseVect.y, radiusReference, radiusReference, SysTime() * 60 )
	
	local iMesh = 1
	local distPar = 0
	local lastPos = mouseVect
	repeat
		distPar = distPar + (lastPos - self.Mesh[iMesh]):Length()
		/*local drawRel = (self.MaxDist - distPar) / self.MaxDist
		
		if drawRel > 0 then
			local drawSize = (0.5 + 0.5 * drawRel) ^ 2 * radiusReference / 4
			
			surface.SetDrawColor( 255, 255, 255, 255*drawRel )
			surface.DrawTexturedRectRotated( self.Mesh[iMesh].x, self.Mesh[iMesh].y, drawSize, 2*drawSize, 90-(lastPos - self.Mesh[iMesh]):Normalize():Angle().y )
			
		end*/
		lastPos = self.Mesh[iMesh]
		
		iMesh = iMesh + 1
	until (distPar >= self.MaxDist) or (iMesh > self.MeshDetail)
	
	local iMeshTotal = iMesh - 1
	render.StartBeam( iMeshTotal )
	for i=1,iMeshTotal do
		local fRel = (i - 1) / (iMeshTotal - 1)
		local fRelRev = 1 - fRel
		local drawSize = (0.5 + 0.5 * fRel) ^ 2 * radiusReference / 4
		
		render.AddBeam( self.Mesh[i], fRelRev * radiusReference, 0.5, 1, self.color )
	end
	render.EndBeam( )
		
end

function ELEMENT.HOOK:PostDrawTranslucentRenderables( )
	self:Perform( )
	
	/*local time = CurTime() / 2
	render.SetMaterial( self.myMaterial )
	
	render.StartBeam( self.NUM_SEGS )
	local fRel = 0
	for i = 1, self.NUM_SEGS do
		fRel = (i - 1) / (self.NUM_SEGS - 1)
		render.AddBeam( self.curl[i], math.sin(fRel * math.pi) * 8, fRel + time % 1, 1, self.color )
		
	end
	render.EndBeam( )*/
	
end


dhlite.RegisterElement( ELEMENT )
